package com.enqualcomm.kids.mvp.fillwatchinfo;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.manager.Controller;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.AddTerminalParams;
import com.enqualcomm.kids.network.socket.request.SaveDetachNoticeParams;
import com.enqualcomm.kids.network.socket.request.SaveSilenceParams;
import com.enqualcomm.kids.network.socket.response.AddTerminalResult;
import com.enqualcomm.kids.network.socket.response.BasicResult;

/**
 * Created by yinzhiqun on 2015/10/12.
 *
 */
public class AddWatchModel extends NetworkModel {


    public void addWatchCid(String cid,String userkey,String userid, EventHandler handler,String mobile,String terminalname,String relation,int height,int weight,int gender,  String mobile2, int mobiletype, String areacode, String areacode2) {
        AddTerminalParams addTerminalParams = new AddTerminalParams("",cid,userid,userkey,mobile,terminalname,relation,height,weight,gender,mobile2,mobiletype,areacode,areacode2);
        send(addTerminalParams,handler);
    }

    public void addWatchWid(String wid,String userkey,String userid, EventHandler handler,String mobile,String terminalname,String relation,int height,int weight,int gender,  String mobile2, int mobiletype, String areacode, String areacode2) {
        AddTerminalParams addTerminalParams = new AddTerminalParams(wid,"",userid,userkey,mobile,terminalname,relation,height,weight,gender,mobile2,mobiletype,areacode,areacode2);
        send(addTerminalParams,handler);
    }


    private void send(AddTerminalParams addTerminalParams, final EventHandler handler) {
        loadDataFromServer(new SocketRequest<AddTerminalResult>(addTerminalParams, new NetworkListener<AddTerminalResult>() {
            @Override
            public void onError(VolleyError error) {
                handler.onNetworkError();
            }

            @Override
            public void onSuccess(AddTerminalResult response) {
                if (response.code == 0) {
                    if (response.result.confirmstate == 1) {
                        if (response.result.isOwner == 1) {
//                            addSilenceAndNotice(response.result.terminalid);//现在又不要求设置静默时段了。。。
                            handler.onSuccessAdmin(response.result.terminalid,response.result.userterminalid,response.result.gps,response.result.relatives);
                        } else {
                            handler.onSuccessOthers();
                        }
                    } else {
                        handler.onError(-1,response.result.ownerphone);
                    }
                } else {
                    handler.onError(response.code,null);
                }
            }
        }));
    }

    private void addSilenceAndNotice(String terminalid){
        AppDefault appDefault = new AppDefault();
        String userid = appDefault.getUserid();
        String userkey = appDefault.getUserkey();
        loadDataFromServer(new SocketRequest<BasicResult>(
                new SaveDetachNoticeParams(userkey, userid, terminalid, "00:00", "23:59", 1, 127), null));


        if (!Controller.getInstance().isOldMainMode()){//科乐老人版本不需要添加默认静默时段！！！
            loadDataFromServer(new SocketRequest<BasicResult>(
                    new SaveSilenceParams(userkey, userid, terminalid, "08:00", "12:00",0,62),null));

            loadDataFromServer(new SocketRequest<BasicResult>(
                    new SaveSilenceParams(userkey, userid, terminalid, "14:00", "17:00",0,62),null));
        }
    }
}
